//
//  DYMainVC.m
//  Run
//
//  Created by sxz on 16/4/1.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "DYMainVC.h"

@interface DYMainVC ()

@property (nonatomic, strong) UIImageView *backgroundImg;//背景
@property (nonatomic, strong) UIButton *beginButton;//开始按钮
@property (nonatomic, strong) UILabel *distancLabel;//距离
@property (nonatomic, strong) UILabel *distancTextLabel;

@property (nonatomic, strong) UILabel *timeLabel;//事件
@property (nonatomic, strong) UILabel *timeTextLabel;

@property (nonatomic, strong) UILabel *numLabel;//次数
@property (nonatomic, strong) UILabel *numTextLabel;

// coreData
@property (nonatomic, strong) CoreDataManager *coreDataManager;
@property (nonatomic, strong) NSMutableArray *coreDataArr;
@property (nonatomic, assign) CGFloat distance;
@property (nonatomic, assign) NSInteger totalTime;


@end

@implementation DYMainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createView];
    
    self.coreDataManager = [CoreDataManager shareCoreDataManager];
    self.coreDataArr = [NSMutableArray arrayWithCapacity:0];
    [self getAllRunData];
    
    self.beginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [_beginButton setTitle:@"开始跑步" forState:UIControlStateNormal];
    _beginButton.frame = CGRectMake(WIDTH/2.757, HEIGHT/1.611,WIDTH/3.409,WIDTH/3.409);
    _beginButton.layer.cornerRadius = WIDTH/6.818;
    _beginButton.clipsToBounds = YES;
    
    [_beginButton addTarget:self action:@selector(beginAction:) forControlEvents:UIControlEventTouchUpInside];
    [_backgroundImg addSubview:_beginButton];


}

- (void)viewWillAppear:(BOOL)animated {
    
    if (self.coreDataManager) {
        [self getAllRunData];
    }
    
}

//开始按钮点击事件
- (void)beginAction:(UIButton *)sender {

    MapVC *mapVC = [[MapVC alloc] init];
    [self.navigationController presentViewController:mapVC animated:YES completion:^{
        
    }];

}

-(void)createView {
//    背景图片
    self.backgroundImg = [[UIImageView alloc] initWithFrame:self.view.bounds];
    _backgroundImg.userInteractionEnabled = YES;
    _backgroundImg.image = [UIImage imageNamed:@"主页背景"];
    [self.view addSubview:_backgroundImg];
   
//    距离
    self.distancLabel =[[UILabel alloc] initWithFrame:CGRectMake(WIDTH/7.5, HEIGHT/6.67, WIDTH/1.363, HEIGHT/6.67)];
    self.distancLabel.textAlignment = NSTextAlignmentCenter;
    self.distancLabel.text = @"0.00";
    self.distancLabel.font = [UIFont boldSystemFontOfSize:70];
    self.distancLabel.textColor = [UIColor colorWithRed:0.6 green:1.0 blue:0.8 alpha:1.0];
    
    [_backgroundImg addSubview:_distancLabel];
    
    self.distancTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/7.5, _distancLabel.frame.origin.y + 110, WIDTH/1.363, 40)];
    _distancTextLabel.text = @"跑步总里程(公里)";
    _distancTextLabel.textColor = [UIColor whiteColor];
    _distancTextLabel.textAlignment =NSTextAlignmentCenter;
    [_backgroundImg addSubview:_distancTextLabel];
    
//    时间
    self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/7.5, _distancTextLabel.frame.origin.y + 50, HEIGHT/6.67, 40)];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.timeLabel.text = @"00:00:00";
    self.timeLabel.font = [UIFont systemFontOfSize:15];
    self.timeLabel.textColor = [UIColor colorWithRed:0.6 green:1.0 blue:0.8 alpha:1.0];
    [_backgroundImg addSubview:_timeLabel];
    
    
    self.timeTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/7.5, _timeLabel.frame.origin.y + 50, HEIGHT/6.67, 20)];
    _timeTextLabel.text = @"累计用时";
    _timeTextLabel.textColor = [UIColor whiteColor];
    _timeTextLabel.textAlignment = NSTextAlignmentCenter;
    [_backgroundImg addSubview:_timeTextLabel];

    
//   次数
    self.numLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/1.666, _distancTextLabel.frame.origin.y + 50, HEIGHT/6.67, 40)];
    self.numLabel.textColor = [UIColor colorWithRed:0.6 green:1.0 blue:0.8 alpha:1.0];
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    self.numLabel.text = @"0次";
    self.numLabel.font = [UIFont systemFontOfSize:15];
    [_backgroundImg addSubview:_numLabel];
    
    self.numTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/1.666, _numLabel.frame.origin.y + 50, HEIGHT/6.67, 20)];
    
//    _numTextLabel.backgroundColor =[UIColor yellowColor];
    _numTextLabel.text = @"运动次数";
    _numTextLabel.textColor = [UIColor whiteColor];
    _numTextLabel.textAlignment =NSTextAlignmentCenter;
    [_backgroundImg addSubview:_numTextLabel];


}

- (void)getAllRunData {
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"RunData" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%@", error);
    } else {
        
        self.coreDataArr = [NSMutableArray arrayWithArray:fetchedObjects];
        
        if (self.coreDataArr.count > 0) {
            
            self.distance = 0;
            self.totalTime = 0;
            
            for (RunData *data in self.coreDataArr) {
                
                self.distance += data.distance.doubleValue;
                self.totalTime += data.time.integerValue;
                
            }
            
            self.numLabel.text = [NSString stringWithFormat:@"%ld次", self.coreDataArr.count];
            self.distancLabel.text = [NSString stringWithFormat:@"%0.2f", self.distance / 1000.0];
            self.timeLabel.text = [self convertTime:self.totalTime];
            
        }
        
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 时间转换
- (NSString *)convertTime:(NSInteger)second {
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    second -= 3600 * 8;
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    [fmt setDateFormat:@"HH:mm:ss"];
    NSString *showtimeNew = [fmt stringFromDate:d];
    return showtimeNew;
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
